package com.twentyOne.ClothingLand.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.twentyOne.ClothingLand.dao.BaseDAO;
import com.twentyOne.ClothingLand.dao.IPaymentDAO;
import com.twentyOne.ClothingLand.vo.Payment;

public class PaymentDAO extends BaseDAO implements IPaymentDAO {

	@Override
	public void addPayment(Payment payment) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		session.save(payment);
		tx.commit();
		session.close();
		
	}

	@Override
	public void modifyPayment(Payment payment) {
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		String hql = "update Payment set amount=:amount where id=:id";
		Query query = session.createQuery(hql);
		query.setDouble("amount", payment.getAmount());
		query.setInteger("id", payment.getId());
		query.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public Payment searchPaymentBySaleOrderNo(String saleOrderNo) {

		Session session = getSession();
		String hql = "from Payment as p where p.saleOrderNo = " + saleOrderNo;
		Query query = session.createQuery(hql);
		List<Payment> list = query.list();
		session.close();
		if (list.size() != 0) {
			Payment payment = list.get(0);
			return payment;
		}else {
			return null;
		}
		
	}

}
